﻿// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using NSQL.Sharpen;

namespace NSQL.Expressions.Functions
{
    public class DecodeFunction : BaseExpression, IDecodeFunction
    {
        public ExpressionCollection Items { get; set; }
        public DecodeFunction(IEnumerable<IExpression> items)
        {
            if (items == null) throw new ArgumentNullException(nameof(items));
            Items = new ExpressionCollection(items);

            if (Items.Count < 3)
            {
                throw new ArgumentException("The DECODE() Function must have at least 3 items.");
            }
        }

        public DecodeFunction(ExpressionCollection items)
        {
            Items = items;
        }

        public override T Accept<T>(IExpressionVisitor<T> visitor)=> visitor.Visit(this);

        public override void Accept(IExpressionVisitor visitor) => visitor.Visit(this);

        public override bool IsValid => Items?.Count >= 3 && Items.IsValid;

        protected override IExpression DeepCopy()
        {
            return new DecodeFunction(Items.DeepClone())
            {
                Tag = Tag
            };
        }

    }
}